package com.xxx.video.commen.utils;

import android.content.Context;
import android.graphics.Typeface;

import java.util.HashMap;

/**
 */
public class FontCacheUtils {
    private static FontCacheUtils mFontCacheUtils = null;
    private HashMap<String, Typeface> fontCache = new HashMap<>();

    private FontCacheUtils() {
    }

    public static FontCacheUtils getInstance() {
        if (mFontCacheUtils == null) {
            synchronized (FontCacheUtils.class) {
                if (mFontCacheUtils == null) {
                    mFontCacheUtils = new FontCacheUtils();
                }
            }
        }
        return mFontCacheUtils;
    }

    /**
     *
     */
    public Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);
        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), fontname);
            } catch (Exception e) {
                return null;
            }
            fontCache.put(fontname, typeface);
        }
        return typeface;
    }
}
